from fastapi import FastAPI, Form
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

app = FastAPI()

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源
    allow_credentials=True,
    allow_methods=["*"],  # 允许所有方法
    allow_headers=["*"],  # 允许所有头
)

@app.post("/login")
async def login(username: str = Form(...), password: str = Form(...)):
    print(f"用户名: {username}")
    print(f"密码: {password}")
    return {"message": "登录信息已接收"}

from fastapi.responses import HTMLResponse

@app.get("/index")
async def index():
    with open("front.html", "r", encoding="utf-8") as file:
        content = file.read()
    return HTMLResponse(content=content)


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8001)

